/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* BaseNationalAreaService
* 基础全国行政区划
*
* 修改纪录
* 2022-01-20 版本：1.0 wangxinhua 创建。
* @version 版本：1.0
* @author 作者：wangxinhua
* 创建日期：2022-01-20
*/
package com.topscomm.base.service.impl;

import com.topscomm.base.pojo.BaseNationalAreaEntity;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.base.service.IBaseNationalAreaService;
import com.topscomm.base.service.impl.auto.BaseNationalAreaServiceAuto;

import java.util.*;
import java.util.stream.Collectors;

@Service(value = "baseNationalAreaService")
@Scope(value = "singleton")
public class BaseNationalAreaService extends BaseNationalAreaServiceAuto implements IBaseNationalAreaService {

    @Override
    public List<Map<String,Object>> areaTreeList() {
        List<String> fields = Arrays.asList(BaseNationalAreaEntity.FieldCode,BaseNationalAreaEntity.FieldName,BaseNationalAreaEntity.FieldParentcode);
        List<Map<String,Object>> allArea = this.queryMapFieldsByWhere(" enabled=1 ",fields);
        List<Map<String,Object>> parentArea = allArea.stream().filter(item -> "00".equals(item.get("code").toString().substring(4))).collect(Collectors.toList());
        List<Map<String,Object>> resList = new ArrayList<Map<String,Object>>();

        for(Map<String,Object> pItem : parentArea) {
            List<Map<String,Object>> children = allArea.stream().filter(item -> pItem.get("code").toString().equals(item.get("parentcode").toString())).collect(Collectors.toList());
            pItem.put("children",children);
        }
        resList = parentArea.stream().filter(item -> "0".equals(item.get("parentcode").toString()))
                .sorted(Comparator.comparing(s->s.get("code").toString())).collect(Collectors.toList());
        return resList;
    }
}
